package com.servlet.form;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.base.Contact;
import com.servlet.model.base.Profile;
import com.servlet.model.base.Study;
import com.servlet.model.database.BaseAcces;

public class SuppressionEtudeForm extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{	
		HttpSession session = request.getSession();
		
		BaseAcces base = ContextListener.base;
		String email = (String) session.getAttribute("email");

		ArrayList<Study> studies = (ArrayList<Study>) (session.getAttribute("studies"));

		int num = Integer.parseInt(request.getParameter("num"));
		
		base.removeStudy(studies.get(num));

		ArrayList<Contact> a = base.findContacts(email);
		
		java.util.Date dd = Calendar.getInstance().getTime();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dat = dateFormat.format(dd);
		Date dateActuelle = Date.valueOf(dat);
		
		for( int i = 0 ; i < a.size() ; i++ )
		{
			if( a.get(i).getIs_accepted() )
			if( a.get(i).getEmail_user1().compareTo(email) == 0 )
				base.addNews( email , a.get(i).getEmail_user2() , dateActuelle , 0 );
			else
				base.addNews( email , a.get(i).getEmail_user1() , dateActuelle , 0 );
		}
		
		session.setAttribute("resultat", "Suppression reussie !! ");

		response.sendRedirect("profil");
	}
}
